﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using AzureServices.Models;

namespace AzureServices.Controllers
{
    public class TaskLogController : ApiController
    {
        private AntaresDBEntities3 db = new AntaresDBEntities3();

        // GET api/TaskLog
        public IEnumerable<TaskLog> GetTaskLogs(int id)
        {
            db.Configuration.ProxyCreationEnabled = false;
            var query = from log in db.TaskLogs
                        where log.TaskID == id
                        select log;

            return query;
        }

        //// GET api/TaskLog/5
        //public TaskLog GetTaskLog(int id)
        //{
        //    TaskLog tasklog = db.TaskLogs.Find(id);
        //    if (tasklog == null)
        //    {
        //        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        //    }

        //    return tasklog;
        //}

        //// PUT api/TaskLog/5
        //public HttpResponseMessage PutTaskLog(int id, TaskLog tasklog)
        //{
        //    if (ModelState.IsValid && id == tasklog.ID)
        //    {
        //        db.Entry(tasklog).State = EntityState.Modified;

        //        try
        //        {
        //            db.SaveChanges();
        //        }
        //        catch (DbUpdateConcurrencyException)
        //        {
        //            return Request.CreateResponse(HttpStatusCode.NotFound);
        //        }

        //        return Request.CreateResponse(HttpStatusCode.OK);
        //    }
        //    else
        //    {
        //        return Request.CreateResponse(HttpStatusCode.BadRequest);
        //    }
        //}

        // POST api/TaskLog
        public HttpResponseMessage PostTaskLog(TaskLog tasklog)
        {
            if (ModelState.IsValid)
            {
                if (!db.TaskLogs.Any())
                {
                    tasklog.ID = 0;
                }
                else
                {
                    tasklog.ID = db.TaskLogs.Max(record => record.ID) + 1;
                }

                tasklog.DateTaken = DateTime.Now;

                db.TaskLogs.Add(tasklog);
                db.SaveChanges();
                db.Configuration.ProxyCreationEnabled = false;
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, tasklog);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = tasklog.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        //// DELETE api/TaskLog/5
        //public HttpResponseMessage DeleteTaskLog(int id)
        //{
        //    TaskLog tasklog = db.TaskLogs.Find(id);
        //    if (tasklog == null)
        //    {
        //        return Request.CreateResponse(HttpStatusCode.NotFound);
        //    }

        //    db.TaskLogs.Remove(tasklog);

        //    try
        //    {
        //        db.SaveChanges();
        //    }
        //    catch (DbUpdateConcurrencyException)
        //    {
        //        return Request.CreateResponse(HttpStatusCode.NotFound);
        //    }

        //    return Request.CreateResponse(HttpStatusCode.OK, tasklog);
        //}

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}